package com.qin.EducationalManger.user.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qin.EducationalManger.user.model.UserModel;
import com.qin.EducationalManger.user.service.UserService;


/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
	 request.setCharacterEncoding("UTF-8");
	 response.setCharacterEncoding("UTF-8");
		
	 String code = request.getParameter("code");
	
	 String authCode = request.getSession().getAttribute("authCode").toString();
 
	  if(code.equals(authCode)) {  //判断验证码是否正确
		    String content ="";
			String action = request.getParameter("action");
			switch (action) {
			case "注册":
				content = register(request);
				response.getWriter().write(content);
				break;
			case "登录":
				content = login(request);
				response.getWriter().write(content);
				break;
			default:
				break;
			}
			
	
    	    	    	 
     }else {
    	 response.getWriter().write("验证码错误");
		
	}
	  		
		
	}
	
	private UserService userService = new UserService();
	
	private String register(HttpServletRequest request) throws UnsupportedEncodingException {
		
		  request.setCharacterEncoding("UTF-8");
		
		  String content ="";
		  
		  String account = request.getParameter("account");
		  String password = request.getParameter("password");  
		  String  teacherId = request.getParameter("teacherId");
		 
		  UserModel model = new UserModel();
			
		  model.setAccount(account);  //只查询账号 是否存在
		  
		  
		  content =  userService.insert(model,password,teacherId); //   返回结果
		  
		  return content;
		  
	}	
	private String login(HttpServletRequest request) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("UTF-8");
		String content ="";

	    String account = request.getParameter("account");
		String password = request.getParameter("password");
 
		 UserModel model = new UserModel();  //查询该账号密码是否一致是否存在
		 model.setAccount(account);
		 

		 content = userService.select(model,password);//返回结果
		 
		 return content;
		 
		 
	}

}
